import AttachFlowPlugin from './main';
import { PluginSettingTab, Setting, App, Notice } from 'obsidian';
import { setDebug } from './util';



export interface AttachFlowSettings {
    deleteOption: string;
    logsModal: boolean;
    dragResize: boolean;
    resizeInterval: number;
    clickView: boolean;
    adaptiveRatio: number;
    moveFileMenu: boolean;
    debug: boolean;
}

export const DEFAULT_SETTINGS: AttachFlowSettings = {
    deleteOption: '.trash',
    logsModal: true,
    dragResize: true,
    resizeInterval: 0,
    clickView: false,
    adaptiveRatio: 0.9,
    moveFileMenu: false,
    debug: false,
};


export class AttachFlowSettingsTab extends PluginSettingTab {

    plugin: AttachFlowPlugin;


    constructor(app: App, plugin: AttachFlowPlugin) {
        super(app, plugin);
        this.plugin = plugin;
    }


    display(): void {
        const { containerEl } = this;
        containerEl.empty();

        new Setting(containerEl).setName('附件的右键菜单').setHeading();
        new Setting(containerEl)
            .setName('已删除附件目标')
            .setDesc('选择附件删除后要移动到的位置')
            .addDropdown((dropdown) => {
                dropdown.addOption('permanent', '永久删除');
                dropdown.addOption('.trash', '移动到应用回收站');
                dropdown.addOption('system-trash', '移动到系统回收站');
                dropdown.setValue(this.plugin.settings.deleteOption);
                dropdown.onChange((option) => {
                    this.plugin.settings.deleteOption = option;
                    this.plugin.saveSettings();
                });
            });

        new Setting(containerEl)
            .setName('移动文件到...')
            .setDesc('在附件的右键菜单中添加“移动到...”选项')
            .addToggle((toggle) => {
                toggle.setValue(this.plugin.settings.moveFileMenu)
                    .onChange(async (value) => {
                        this.plugin.settings.moveFileMenu = value;
                        await this.plugin.saveSettings();
                    });
            });

        new Setting(containerEl).setName('点击查看图片').setHeading();
        new Setting(containerEl)
            .setName("点击查看图片")
            .setDesc("点击图片右半部分可查看图片详情")
            .addToggle((toggle) => {
                toggle.setValue(this.plugin.settings.clickView)
                    .onChange(async (value) => {
                        this.plugin.settings.clickView = value;
                        await this.plugin.saveSettings();
                    });
            });
        
        new Setting(containerEl)
            .setName('基于窗口大小的自适应图像显示比例')
            .setDesc('当图像超出窗口大小时，图像会根据窗口大小自适应显示')
            .addSlider((slider) => {
                slider.setLimits(0.1, 1, 0.05);
                slider.setValue(this.plugin.settings.adaptiveRatio);
                slider.onChange(async (value) => {
                    this.plugin.settings.adaptiveRatio = value;
                    new Notice(`自适应比率： ${value}`);
                    await this.plugin.saveSettings();
                });
                slider.setDynamicTooltip();
            });

        new Setting(containerEl).setName('拖动以调整图像大小').setHeading();
        new Setting(containerEl)
            .setName("拖动以调整图像大小")
            .setDesc("启用此功能可通过拖动来调整图像大小")
            .addToggle((toggle) => {
                toggle.setValue(this.plugin.settings.dragResize)
                    .onChange(async (value) => {
                        this.plugin.settings.dragResize = value;
                        await this.plugin.saveSettings();
                    });
            });

        new Setting(containerEl)
            .setName("调整间隔")
            .setDesc("拖拽调节最小刻度（默认值为 0 即不对齐刻度）")
            .addText((text) => {
                text.setValue(this.plugin.settings.resizeInterval.toString())
                    .onChange(async (value) => {
                        // 判断输入值是否为正整数
                        if (value === '') {
                            // Input is empty. Set to default value 0
                            this.plugin.settings.resizeInterval = 0;
                            await this.plugin.saveSettings();
                        }else if (/^\d+$/.test(value) && Number(value) >= 0) {
                            this.plugin.settings.resizeInterval = parseInt(value);
                            await this.plugin.saveSettings();
                        } else {
                            // 不符合要求时，可以给出提示
                            new Notice('请输入正整数');
                            text.setValue(this.plugin.settings.resizeInterval.toString()); // 重新设置为原来的值
                        }
                    });
            });

        new Setting(containerEl).setName('调试').setHeading();
        new Setting(containerEl)
            .setName("调试模式")
            .setDesc("在控制台打印调试信息")
            .addToggle((toggle) => {
                toggle.setValue(this.plugin.settings.debug)
                    .onChange(async (value) => {
                        this.plugin.settings.debug = value;
                        setDebug(value);
                        await this.plugin.saveSettings();
                    });
            });
    }
}
